package com.passbook.merchant.security;


//import com.auth0.jwt.JWT;
//import com.auth0.jwt.exceptions.JWTDecodeException;
//import com.google.gson.Gson;
//import com.passbook.common.constant.Constants;
//import com.passbook.merchant.util.JWTUtil;
//import com.passbook.merchant.vo.PassTemplateVO;
import com.passbook.common.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
//import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedReader;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.lang.reflect.Method;
//import java.nio.charset.Charset;
//import java.util.HashMap;
//import java.util.Map;

public class AuthCheckInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        String token = request.getHeader("token");
//        InputStream inputStream = request.getInputStream();
//        StringBuilder stringBuilder = new StringBuilder();
//        BufferedReader reader = null;
//        reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
//        String line;
//        while ((line = reader.readLine()) != null) {
//            stringBuilder.append(line);
//        }
//        String bodyData = stringBuilder.toString();
//
//        Map<String, String> bodymap = new HashMap<>();
//        Gson gson = new Gson();
//        bodymap = gson.fromJson(bodyData, bodymap.getClass());
//        String merchantId = (String) bodymap.get("id");
//
//        if (!(o instanceof HandlerMethod)) {
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod)o;
//        Method method = handlerMethod.getMethod();
//        // 检测请求中是否有注解，没有注解则不需要jwt认证
//        if (method.isAnnotationPresent(PassToken.class)) {
//            PassToken passToken = method.getAnnotation(PassToken.class);
//            if (passToken.required()) {
//                return true;
//            }
//        }
//        if (method.isAnnotationPresent(AuthorizationToken.class)) {
//            AuthorizationToken authorizationToken = method.getAnnotation(AuthorizationToken.class);
//            if (token == null) {
//                String jwt = JWTUtil.genToken(merchantId);
//                throw new Exception("Header 中缺少" + Constants.TOKEN_STRING + "!");
//            }
//        }
//        try {
//            String authmerchantId = JWT.decode(token).getAudience().get(0);
//        } catch (JWTDecodeException j) {
//            throw new RuntimeException("401");
//        }

        String token = request.getHeader(Constants.TOKEN_STRING);
        if (StringUtils.isEmpty(token)) {
            throw new Exception("Header 中缺少" + Constants.TOKEN_STRING + "!");
        }
        if (!token.equals(Constants.TOKEN)) {
            throw new Exception("Header 中" + Constants.TOKEN_STRING + "错误！");
        }
        AccessContext.setToken(token);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           @Nullable ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object o, Exception ex) throws Exception {

        AccessContext.clearAccessKey();
    }
}
